using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Typing_GUI.CLASS;
using System.Collections;
namespace Typing_GUI
{
    public partial class frmEditExam : Form
    {
        public frmEditExam()
        {
            InitializeComponent();
        }
        private int m_MaLuyenTap;

        public int MaLuyenTap
        {
            get { return m_MaLuyenTap; }
            set { m_MaLuyenTap = value; }
        }

        public ArrayList mangBaitapTrongExam = new ArrayList();
        public ArrayList mangBaiTapTrongExamCu = new ArrayList();
        public bool KiemTraDuLieuDauVao()
        {

            if (txtTenExam.Text == "")
            {
                MessageBox.Show("Exam Name is  blank !! ", "ERROR !!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtTenExam.Focus();
                return false;
            }
            else if (txtMoTa.Text == "")
            {
                MessageBox.Show("you must write some in Decription  !! ", "Warning  !!", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
                txtMoTa.Focus();
                return false;
            }

            //else if (dgvDanhSachBaiTapCuaExam.Rows.Count <1)
            //{
            //    MessageBox.Show("you must add some exercise in List Exam !! ", "Warning  !!", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            //    dgvDanhSachBaiTapCuaExam.Focus();
            //    return false;          
            //}

            return true;
        }

        public bool KiemTraBaiTapTrongExam(CBaiTap bt)
        {
            for (int i = 0; i < mangBaitapTrongExam.Count; i++)
            {
                CBaiTap temp = (CBaiTap)mangBaitapTrongExam[i];
                if (temp.Mabaitap == bt.Mabaitap)
                    return false;
            }
            return true;
        }

        public void DanhSachBaiTapCuaExam()
        {
            try
            {
                DataTable dt = CToChucLuyenTap.LayDanhSachBaiTapCuaToChucLuyenTap(MaLuyenTap);
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    CBaiTap bt = new CBaiTap();
                    bt.Mabaitap = int.Parse(dt.Rows[i][0].ToString());
                    bt.Tenbaitap = dt.Rows[i][1].ToString();
                    mangBaitapTrongExam.Add(bt);
                    mangBaiTapTrongExamCu.Add(bt);
                }
            }
            catch
            {
                MessageBox.Show("Error connecting database ", "ERROR !!", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }

        public void HienThiDanhSachBaiTapCuaExam()
        {
            
            for (int i = 0; i < mangBaitapTrongExam.Count; i++)
            {
                CBaiTap bt = (CBaiTap)mangBaitapTrongExam[i];
                DataGridViewRowCollection r = this.dgvDanhSachBaiTapCuaExam.Rows;
                r.Add((i + 1).ToString(),bt.Mabaitap, bt.Tenbaitap);
            }
            dgvDanhSachBaiTapCuaExam.SelectionMode = DataGridViewSelectionMode.CellSelect;
            dgvDanhSachBaiTapCuaExam.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
        }
        public void DanhSachTatCaBaiTap()
        {
            try
            {
                DataTable dt = CBaiTap.LoadDanhSachBaiTap(1);

                int stt = 1;
                foreach (DataRow row in dt.Rows)
                {
                    DataGridViewRowCollection r = this.dgvDanhSachBaiTap.Rows;
                    r.Add(stt.ToString(), row["MaBaiTap"], row["TenBaiTap"]);
                    stt++;
                }
                dgvDanhSachBaiTap.SelectionMode = DataGridViewSelectionMode.CellSelect;
                dgvDanhSachBaiTap.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            }
            catch
            {
                MessageBox.Show("Error connecting database ", "ERROR !!", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void LoadLaiDanhSachExam()
        {
            foreach (Form frm in Application.OpenForms)
            {
                if (frm is frmManageExam)
                {
                    ((frmManageExam)frm).LoadDuLieuCuaExamList();
                    break;
                }
            }
            this.Close();
        }
        private void frmEditExam_Load(object sender, EventArgs e)
        {
            DanhSachTatCaBaiTap();
            DataTable dt = CToChucLuyenTap.LayThongTinCuaToChucLuyenTap(MaLuyenTap);
            txtTenExam.Text = dt.Rows[0][0].ToString();
            txtMoTa.Text = dt.Rows[0][1].ToString();

            DanhSachBaiTapCuaExam();
            HienThiDanhSachBaiTapCuaExam();
            

        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            if (dgvDanhSachBaiTap.SelectedRows.Count > 0)
            {
                if (KiemTraDuLieuDauVao() == true)
                {
                    for (int i = dgvDanhSachBaiTap.SelectedRows.Count - 1; i >= 0; i--)
                    {
                        CBaiTap bt = new CBaiTap();
                        bt.Mabaitap = (int)dgvDanhSachBaiTap.SelectedRows[i].Cells[1].Value;
                        bt.Tenbaitap = dgvDanhSachBaiTap.SelectedRows[i].Cells[2].Value.ToString();
                        if (KiemTraBaiTapTrongExam(bt) == true)
                            mangBaitapTrongExam.Add(bt);
                        else
                        {
                            MessageBox.Show("This Exercise is exists !! :  " + bt.Tenbaitap, "ERROR !!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                    }
                }
            }
            else
            {
                MessageBox.Show("Not choose exercise to add in a exam ", "ERROR !!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            dgvDanhSachBaiTapCuaExam.Rows.Clear();
            HienThiDanhSachBaiTapCuaExam();
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnXoaBaiTap_Click(object sender, EventArgs e)
        {
            if (dgvDanhSachBaiTapCuaExam.SelectedRows.Count > 0)
            {
                for (int i = dgvDanhSachBaiTapCuaExam.SelectedRows.Count - 1;i>=0 ; i--)
                {
                    int index = dgvDanhSachBaiTapCuaExam.SelectedRows[i].Index;
                    DialogResult dl = MessageBox.Show("Are you sure you want to delete it :" + dgvDanhSachBaiTapCuaExam.SelectedRows[i].Cells[2].Value.ToString(), "Warning !!!", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
                    if (dl == DialogResult.OK)
                        mangBaitapTrongExam.RemoveAt(index);
                }
                dgvDanhSachBaiTapCuaExam.Rows.Clear();
                HienThiDanhSachBaiTapCuaExam();
            }
            else
                MessageBox.Show("Not choose exercise to delete  ", "Warning!!", MessageBoxButtons.OK, MessageBoxIcon.Warning); 
      
        }

        private void btnCapNhat_Click(object sender, EventArgs e)
        {
            int flag=-1;
            int index=-1;
            for (int i = 0; i < mangBaiTapTrongExamCu.Count; i++)
            {
                flag=0;
                for (int j = 0; j < mangBaitapTrongExam.Count; j++)
                {
                    if (mangBaiTapTrongExamCu[i] == mangBaitapTrongExam[j])
                    {
                        flag = 1;
                        index = j;
                    }
                }
                if (flag == 1)
                {
                    mangBaitapTrongExam.RemoveAt(index);
                }
                else
                {
                    CBaiTap bt = (CBaiTap)mangBaiTapTrongExamCu[i];
                    CChiTietLuyenTap chiTiet = new CChiTietLuyenTap();
                    chiTiet.Mabaitap = bt.Mabaitap;
                    chiTiet.Maluyentap = MaLuyenTap;
                    chiTiet.XoaChiTiet1LuyenTap();
                }
            }


            if (KiemTraDuLieuDauVao())
            {
                try
                {
                    CToChucLuyenTap toChuc = new CToChucLuyenTap();
                    toChuc.Maluyentap = MaLuyenTap;
                    toChuc.Tenluyentap = txtTenExam.Text;
                    toChuc.Mota = txtMoTa.Text;
                    //  toChuc.createTCLT(); co van de
                    toChuc.updateTCLT();
                   
                    for (int i = 0; i < mangBaitapTrongExam.Count; i++)
                    {
                        CBaiTap bt = (CBaiTap)mangBaitapTrongExam[i];
                        CChiTietLuyenTap chiTiet = new CChiTietLuyenTap();
                        chiTiet.Mabaitap = bt.Mabaitap;
                        chiTiet.Maluyentap = MaLuyenTap;
                        chiTiet.createUnit();
                    }
                    MessageBox.Show("Update  successful \n Check in ManageExam ", "Success !!", MessageBoxButtons.OK, MessageBoxIcon.None);
                    this.Close();
                    txtTenExam.Text = "";
                    txtMoTa.Text = "";
                    txtTenExam.Focus();
                    dgvDanhSachBaiTapCuaExam.Rows.Clear();

                }
                catch
                {
                    MessageBox.Show("Error connecting database ", "ERROR !!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                LoadLaiDanhSachExam();
            }

        }

        
    }
}